﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DALL.SysManage
{
    public class T_PUB_SECURITY
    {
        public T_PUB_SECURITY()
        { }
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_SECURITY model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_SECURITY(");
            strSql.Append("SECURITY,PRODUCER_ID,PRODUCER_NAME,PRODUCE_TIME)");

            strSql.Append(" values (");
            strSql.Append("@SECURITY,@PRODUCER_ID,@PRODUCER_NAME,@PRODUCE_TIME)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "SECURITY", DbType.String, model.SECURITY);
            db.AddInParameter(dbCommand, "PRODUCER_ID", DbType.Int32, model.PRODUCER_ID);
            db.AddInParameter(dbCommand, "PRODUCER_NAME", DbType.String, model.PRODUCER_NAME);
            db.AddInParameter(dbCommand, "PRODUCE_TIME", DbType.DateTime, model.PRODUCE_TIME);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_SECURITY model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_SECURITY set ");
            strSql.Append("SECURITY=@SECURITY,");
            strSql.Append("PRODUCER_ID=@PRODUCER_ID,");
            strSql.Append("PRODUCER_NAME=@PRODUCER_NAME,");
            strSql.Append("PRODUCE_TIME=@PRODUCE_TIME");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "SECURITY", DbType.String, model.SECURITY);
            db.AddInParameter(dbCommand, "PRODUCER_ID", DbType.Int32, model.PRODUCER_ID);
            db.AddInParameter(dbCommand, "PRODUCER_NAME", DbType.String, model.PRODUCER_NAME);
            db.AddInParameter(dbCommand, "PRODUCE_TIME", DbType.DateTime, model.PRODUCE_TIME);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_SECURITY ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_SECURITY GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SECURITY,PRODUCER_ID,PRODUCER_NAME,PRODUCE_TIME from T_PUB_SECURITY ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.SysManage.T_PUB_SECURITY model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_SECURITY GetModel()
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 ID,SECURITY,PRODUCER_ID,PRODUCER_NAME,PRODUCE_TIME from T_PUB_SECURITY order by ID desc ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            Model.SysManage.T_PUB_SECURITY model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SECURITY,PRODUCER_ID,PRODUCER_NAME,PRODUCE_TIME ");
            strSql.Append(" FROM T_PUB_SECURITY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

      
        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_SECURITY> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SECURITY,PRODUCER_ID,PRODUCER_NAME,PRODUCE_TIME ");
            strSql.Append(" FROM T_PUB_SECURITY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_SECURITY> list = new List<Model.SysManage.T_PUB_SECURITY>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_SECURITY ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_SECURITY model = new Model.SysManage.T_PUB_SECURITY();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            model.SECURITY = dataReader["SECURITY"].ToString();
            ojb = dataReader["PRODUCER_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PRODUCER_ID = (int)ojb;
            }
            model.PRODUCER_NAME = dataReader["PRODUCER_NAME"].ToString();
            ojb = dataReader["PRODUCE_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PRODUCE_TIME = (DateTime)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
